package com.itfreer.data.dictionary;

import com.itfreer.data.dictionary.base.Dictionary;
import com.itfreer.data.dictionary.base.DictionaryItem;

import java.util.ArrayList;
import java.util.Comparator;
import java.util.LinkedHashMap;
import java.util.List;
import java.util.Map;

/**
 * 定义字典管理接口
 */
public class DictionaryManage {

    public static final DictionaryManage instance = new DictionaryManage();
    private DictionaryManage(){
    }

    /**
     * 注册的字典值
     */
	private List<Dictionary> dictionarys = new ArrayList<>();

    /**
     * 注册字典
     * @param dic
     */
	public void register(Dictionary dic){
        dictionarys.add(dic);
		dictionarys.sort(new Comparator<Dictionary>() {
			@Override
			public int compare(Dictionary o1, Dictionary o2) {
			    if(o1.getIndex()>o2.getIndex()){
			        return 1;
                }else{
			        return -1;
                }
			}
		});
    }

    /**
     * 获取字典值
     * @param dicName
     * @param id
     * @return
     */
	public String getText(String dicName, String id) {
        if(dictionarys==null || dictionarys.size()<=0){
            return  null;
        }
		if (dicName == null) {
            return null;
        }
        for(Dictionary item : dictionarys){
            String text = item.getText(dicName, id);
            if (text != null) {
                return text;
            }
        }
		return null;
	}

    /**
     * 获取类似于 '0200', '非林地', '0111','乔木林' 的所有字典值
     * @param dicName
     * @return
     */
	public String getDictionaryString(String dicName){
        if(dictionarys==null || dictionarys.size()<=0){
            return  null;
        }
        if (dicName == null) {
            return null;
        }
        for(Dictionary item : dictionarys){
            String text = item.getDictionaryString(dicName);
            if (text != null) {
                return text;
            }
        }
        return null;
    }

    /**
     * 获取字典项
     * @param dicName
     * @param id
     * @return
     */
	public DictionaryItem getDictionary(String dicName, String id) {
        if(dictionarys==null || dictionarys.size()<=0){
            return  null;
        }

        if (dicName == null) {
            return null;
        }
        for(Dictionary item : dictionarys){
            DictionaryItem text = item.getDictionary(dicName, id);
            if (text != null) {
                return text;
            }
        }
        return null;
	}

    /**
     * 根据父ID获取子集
     * @param getNoValidValue 是否获取无效的值
     * @return
     */
    public Map<String, Object> getChildEx(String dicName, String pid, Boolean getNoValidValue) {
        if(dictionarys==null || dictionarys.size()<=0){
            return  null;
        }

        if (dicName == null) {
            return null;
        }
        for(Dictionary item : dictionarys){
            List<DictionaryItem> texts = item.getChild(dicName, pid, getNoValidValue);
            if (texts != null) {
                Map<String, Object> result = new LinkedHashMap<>();
                for(DictionaryItem dic : texts) {
                    result.put(dic.getId(), dic.getDisplay());
                }
                return result;
            }
        }
        return null;
    }

    /**
     * 根据父ID获取子集
     * @param getNoValidValue 是否获取无效的值
     * @return
     */
	public List<DictionaryItem> getChild(String dicName, String pid, Boolean getNoValidValue) {
        if(dictionarys==null || dictionarys.size()<=0){
            return  null;
        }

        if (dicName == null) {
            return null;
        }
        for(Dictionary item : dictionarys){
            List<DictionaryItem> text = item.getChild(dicName, pid, getNoValidValue);
            if (text != null) {
                return text;
            }
        }
        return null;
	}

    /**
     * 根据父ID获取多级子集
     *
     * @param pid
     * @param level
     *            获取多少级字典
     * @param getNoValidValue
     *            是否获取无效的值
     * @return
     */
    public Map<String, Object> getChildrensEx(String dicName, String pid, int level, Boolean getNoValidValue) {
        List<DictionaryItem> first = getChild(dicName, pid, getNoValidValue);
        if (first == null){
            return null;
        }

        Map<String, Object> result = new LinkedHashMap<>();
        int cLevel = 2;
        for (DictionaryItem cItem : first) {
            result.put(cItem.getId(), cItem.getDisplay());

            getChildsEx(result, cItem, dicName, getNoValidValue, cLevel, level);
        }
        return result;
    }

    private void getChildsEx(Map<String, Object> result, DictionaryItem item, String dicName, Boolean getNoValidValue, int cLevel, int level) {
        if (cLevel > level){
            return;
        }

        List<DictionaryItem> cList = getChild(dicName, item.getId(), getNoValidValue);
        if (cList == null){
            return;
        }

        int cl = (cLevel - 1) * 4;
        String sp = "";
        for(int i=1;i<=cl;i++){
            sp += " ";
        }

        for (DictionaryItem cItem : cList) {
            result.put(cItem.getId(), sp + cItem.getDisplay());
            getChilds(cItem, dicName, getNoValidValue, cLevel + 1, level);
        }
    }

	/**
	 * 根据父ID获取多级子集
	 * 
	 * @param pid
	 * @param level
	 *            获取多少级字典
	 * @param getNoValidValue
	 *            是否获取无效的值
	 * @return
	 */
	public List<DictionaryItem> getChildrens(String dicName, String pid, int level, Boolean getNoValidValue) {
		List<DictionaryItem> first = getChild(dicName, pid, getNoValidValue);
		if (first == null)
			return first;

		int cLevel = 2;
		for (DictionaryItem cItem : first) {
			getChilds(cItem, dicName, getNoValidValue, cLevel, level);
		}

		return first;
	}

	private void getChilds(DictionaryItem item, String dicName, Boolean getNoValidValue, int cLevel, int level) {
		if (cLevel > level)
			return;

		List<DictionaryItem> cList = getChild(dicName, item.getId(), getNoValidValue);
		if (cList == null)
			return;

		item.getChildren().addAll(cList);
		for (DictionaryItem cItem : cList) {
			getChilds(cItem, dicName, getNoValidValue, cLevel + 1, level);
		}
	}
}